package com.asf.sofom.controller;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.asf.sofom.domain.AltaWeb;
import com.asf.sofom.domain.FormBuscar;
import com.asf.sofom.domain.OAuthorities;
import com.asf.sofom.domain.OPersona;
import com.asf.sofom.domain.OUsers;
import com.asf.sofom.repository.UsersDao;
import com.asf.sofom.service.AltaWebManager;
import com.asf.sofom.service.SimplePersonaManager;

@Controller
@SessionAttributes
@RequestMapping(value="/altaweb")
public class AltaWebFormController {
	
	@Autowired
	private AltaWebManager altaWebManager;
	@Autowired
	private UsersDao usersDao;
	@Autowired
	private SimplePersonaManager simplePersonaManager;
	
	@RequestMapping(method = RequestMethod.POST, params="Buscar")
	public String buscarUsuario(AltaWeb altaWeb, BindingResult result, Model m){
		OUsers user = new OUsers();
		OPersona persona = new OPersona();
		persona.setId(Integer.parseInt(altaWeb.getVal_buscar()));
		List<OPersona> rp = simplePersonaManager.buscaPersona(persona);
		if(rp.isEmpty()){
			System.out.println("NO HAY RESULTADOS EN PERSONAS");
			return "/altaweb";
		}
		persona = rp.get(0);
		user.setId_persona(altaWeb.getVal_buscar());
		List<OUsers> res = altaWebManager.buscaAltaList(user);
		if(!res.isEmpty()){
			user = res.get(0);
			altaWeb.setUsername(user.getUsername());
//			System.out.println("NO HAY RESULTADOS EN USERS");
//			return "/altaweb";
		}
		altaWeb.setId_persona(persona.getId().toString());
		altaWeb.setNombre(persona.getNombre());
		altaWeb.setApellidop(persona.getApellido_p());
		altaWeb.setApellidom(persona.getApellido_m());
		System.out.println("Buscar" + m.toString());
		return"/altaweb";
	}
	
	@RequestMapping(method = RequestMethod.POST, params="Guardar")
	public String altaUsuario(AltaWeb altaWeb, BindingResult result){
		OUsers user = new OUsers();
		OAuthorities auth = new OAuthorities();
		String pwdSha = "";
		user.setId_persona(altaWeb.getVal_buscar());
		user.setUsername(altaWeb.getUsername());
		auth.setUsername(altaWeb.getUsername());
		auth.setAuthority("ROLE_USER");
		if(altaWeb.getPassword().equals(altaWeb.getCpassword())){
			pwdSha = sha1(altaWeb.getPassword());
			System.out.println(pwdSha);
			user.setPassword(pwdSha);	
		}
		else{
			System.out.println("LOS PASSWORD NO COINCIDEN");
			return "/altaweb";
		}
		user.setEnabled(1);
		altaWebManager.guardaAlta(user);
		altaWebManager.guardaAuthority(auth);
		System.out.println("Guardar");
		return"/altaweb";
	}
	
	@RequestMapping(method = RequestMethod.POST, params = "Cancelar")
	public String cancelar(){
		return "redirect:admin";
	}	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView loadFormAltaWeb() {
		FormBuscar buscar = new FormBuscar();
		AltaWeb alta = new AltaWeb();
		ModelAndView mv = new ModelAndView();
		mv.setViewName("altaweb");
		mv.addObject("buscaUsuario", buscar);
		mv.addObject("altaWeb", alta);
		return mv;
	}
	
	
	public void setAltaWebManager(AltaWebManager altaWebManager){
		this.altaWebManager = altaWebManager;
	}
	
	public AltaWebManager getAltaWebManager(){
		return this.altaWebManager;
	}
	
	public String sha1(String pwd){
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			byte[] res = md.digest(pwd.getBytes());
			StringBuffer sb = new StringBuffer();
			for(int i = 0; i < res.length; i++){
				sb.append(Integer.toString((res[i] & 0xff) + 0x100, 16).substring(1));
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
